package com.henry.imkit

import com.henry.imkit.request.IMRequest
import java.io.File

object IMManagerUtils {

    fun sendText(text: String, dgid: String?,
                 success: OnSendSuccessCallback? = null,
                 error: OnErrorCallback? = null) {
        val msg = IMMessage()
        val elem = IMTextElem()
        elem.text = text
        msg.addElem(elem)
        IMManager.sendMessage(msg, dgid, success, error)
    }

    fun sendImage(path: String, dgid: String?,
                  success: OnSendSuccessCallback? = null,
                  error: OnErrorCallback? = null) {
        // 上传
        val file = File(path)

        IMRequest.uploadChatImages(listOf(file), dgid, {

             for (item in it) {

                 val imageArray = mutableListOf<IMImage>()
                 val image = IMImage()
                 image.type = IM_IMAGE_TYPE_ORIGIN
                 image.width = item.width
                 image.height = item.height
                 image.url = item.url.toString()

                 val thumbImage = IMImage()
                 thumbImage.type = IM_IMAGE_TYPE_THUMB
                 thumbImage.width = item.width
                 thumbImage.height = item.height
                 thumbImage.url = item.thumbUrl.toString()

                 imageArray.add(image)
                 imageArray.add(thumbImage)

                 val msg = IMMessage()
                 val elem = IMImageElem()
                 elem.imagearray = imageArray
                 msg.addElem(elem)
                 IMManager.sendMessage(msg, dgid, success, error)
             }

        }, error)
    }

    // duration毫秒
    fun sendAudio(path: String, duration: Int, dgid: String?,
                  success: OnSendSuccessCallback? = null,
                  error: OnErrorCallback? = null) {
        // 上传
        val file = File(path)
        IMRequest.uploadChatAudio(file, dgid, {
            val msg = IMMessage()
            val elem = IMSoundElem()
            elem.url = it
            elem.duration = duration
            msg.addElem(elem)
            IMManager.sendMessage(msg, dgid, success, error)
        }, error)

    }

    fun sendCustom(data: String, dgid: String?,
                   success: OnSendSuccessCallback? = null,
                   error: OnErrorCallback? = null) {

        val msg = IMMessage()
        val elem = IMCustomElem()
        elem.data = data
        msg.addElem(elem)
        IMManager.sendMessage(msg, dgid, success, error)
    }

    fun sendCustomData(data: Any?, dgid: String?,
                   success: OnSendSuccessCallback? = null,
                   error: OnErrorCallback? = null) {
        IMManager.sendMessage(data, dgid, success, error)
    }

}